<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    var reverseStr = function (s, k) {
      let spliceLen = 2 * k; // 需要等份切割得长度
      let str = ""; // 返回值
      let sLen = s.length;
      let spliceNum = Math.ceil(sLen / spliceLen); // 最多需要循环得次数，输入s长度为5，k为2，只需要循环2次
      for (let i = 0; i < spliceNum; i++) {
        let waitStr = s.substr(i * spliceLen, spliceLen); // 每次循环等待处理的片段
        if (i < spliceNum - 1 || waitStr.length >= k) {
          // 这里是完整的切割 或者 剩余字符长度大于等于k的切割
          let left = waitStr.substr(0, k);
          let right = waitStr.substr(k);
          str += left.split("").reverse().join("");
          str += right;
        } else {
          // 小于k的直接按题意，全部反转即可
          str += waitStr.split("").reverse().join("");
        }
      }
      return str;
    };
    console.log("res: ", reverseStr("abcdefg", 2));
  </script>
</html>
